<?php

abstract class AbstractChecker {

    /**
     * @var ErrorsCollector
     */
    private $oCollector;

    public function __construct(ErrorsCollector $oCollector = null) {
        if ($oCollector === null) $oCollector = new ErrorsCollector();
        $this->oCollector = $oCollector;
    }

    public function throwExceptionIfNeeds() {
       if ($this->oCollector->hasErrors())
           throw new CheckerException($this->oCollector);
    }

    public function addCustomError($msg) { $this->addError($msg); }

    protected function addError($msg) {
        $this->oCollector->addError($msg);
    }

    // predefined

    public function checkCaptcha(Captcha $oCaptcha, $code) {
        $oCaptcha->checkCodeForErrors($code, $this->oCollector);
    }

}

class CheckerException extends Exception {

    /**
     * @var ErrorsCollector
     */
    private $oCollector;

    public function __construct(ErrorsCollector $oCollector) {
        $this->oCollector = $oCollector;
        parent::__construct($oCollector->getErrorsAsText(), 0);
    }

    public function getHtmlMessage() {
        return $this->oCollector->getErrorsAsHtmlList();
    }
}

?>